package com.mmall.util;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class FtpUtil {
    private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);

    private  static  String IP = PropertiesUtil.getProperty("");
    private  static  String FTPUSER = PropertiesUtil.getProperty("");
    private  static  String FTPPASS= PropertiesUtil.getProperty("");

    private String ip;

    private Integer port;

    private String ftpUser;

    private String ftpPass;

    private FTPClient ftpClient;

    public FtpUtil(String ip, Integer port, String ftpUser, String ftpPass) {
        this.ip = ip;
        this.port = port;
        this.ftpUser = ftpUser;
        this.ftpPass = ftpPass;
    }

    public static boolean uploadFile(List<File> fileList) throws IOException{
     FtpUtil ftpUtil = new FtpUtil(IP,21,FTPUSER,FTPPASS);
        logger.info("开始连接ftp服务器");
        boolean result = ftpUtil.remoteUploadFile("img",fileList);
        logger.info("结束上传");
        return result;
    }
     public  boolean remoteUploadFile(String remotePath,List<File> fileList) throws IOException{
         boolean  uploadfile = true;
         FileInputStream  inputStream = null;
         if(ConnectServer(this.ip,this.ftpUser,this.ftpPass,this.port)){
             try {
                 ftpClient.changeWorkingDirectory(remotePath);
                 ftpClient.setBufferSize(1024);
                 ftpClient.setControlEncoding("UTF-8");
                 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                 ftpClient.enterLocalActiveMode();
                 for (File fileitem:fileList){
                     inputStream = new FileInputStream(fileitem);
                     ftpClient.storeFile(fileitem.getName(),inputStream);
                 }

             } catch (IOException e) {
                 logger.error("上传文件异常");
                 e.printStackTrace();
                 uploadfile=false;
             }finally {
                      inputStream.close();
                     ftpClient.disconnect();
             }
         }
         return uploadfile;
    }
    private boolean ConnectServer(String ip,String  user, String pass,Integer port){
   ftpClient = new FTPClient();
   boolean issuccess = false;
        try {
            ftpClient.connect(IP);
            issuccess=ftpClient.login(user,pass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return issuccess;
    }
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getFtpUser() {
        return ftpUser;
    }

    public void setFtpUser(String ftpUser) {
        this.ftpUser = ftpUser;
    }

    public String getFtpPass() {
        return ftpPass;
    }

    public void setFtpPass(String ftpPass) {
        this.ftpPass = ftpPass;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }


}
